package com.codebattery.controller;

import com.codebattery.ApiJsonObject;
import com.codebattery.ApiJsonProperty;
import com.codebattery.domain.entity.Member;
import com.codebattery.domain.model.MemberOrderInfo;
import com.codebattery.domain.model.UserLoginInfo;
import com.codebattery.service.MemberService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;


@RestController
@RequestMapping("/member")
@Slf4j
public class MemberController {
    @Resource
    private MemberService memberService;

    @ApiOperation(value="获取用户",notes="")
    @RequestMapping(value = "/get-member", method = RequestMethod.GET, produces = "application/json")
    public Member getMember(Long memberId) throws Exception {
        Member member = memberService.get(memberId);
        return member;
    }

    @ApiOperation(value="模板示例",notes="")
    @RequestMapping(value = "/test", method = RequestMethod.GET, produces = "application/json")
    public String createMember(Member member) throws Exception {
        memberService.createMember(member);
        return null;
    }

    @ApiOperation(value="用户使用手机号码登录",notes="")
    @RequestMapping(value = "/login", method = RequestMethod.POST, produces = "application/json")
    public UserLoginInfo login(HttpServletRequest request,
                                @ApiJsonObject(name = "login-param", value = {
                                @ApiJsonProperty(key = "username", example = "12345678910", description = "手机号码", type = "string", required = true),
                                @ApiJsonProperty(key = "password", example = "123456", description = "用户密码", type = "string", required = false)})
                        @RequestBody Map param) throws Exception {
        log.info("用户已经登录");

        String mobile = param.get("username").toString();
        String password = param.get("password").toString();
        /*
         * 获取用户信息
         */
        UserLoginInfo userLoginInfo = memberService.login(request, mobile, password);

        return userLoginInfo;
    }

    @ApiOperation(value="模板示例",notes="")
    @RequestMapping(value = "/orders", method = RequestMethod.GET, produces = "application/json")
    public Page<MemberOrderInfo> getMemberOrders(String memberId) throws Exception {
        Page<MemberOrderInfo> memeberOrders = memberService.getMemeberOrders(memberId);
        return memeberOrders;
    }
}
